/*
 * Toedo
 * Copyright (C) 2009-2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.toedo.gui.wizards.htmlexport;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Text;
import org.toedo.gui.translations.Messages;
import org.toedo.logging.Log;
import org.toedo.model.export.HtmlGenerator;

public class HtmlExportCssPage extends WizardPage {
	
	private Composite mContainer;
	
	private Button mDefaultCssButton;
	private Button mNoneCssButton;
	private Button mCustomCssButton;
	
	private Button mImportDefaultCss;
	private Button mLoadCss;
	
	private Text mCustomCssText;
	
	public HtmlExportCssPage() {
		super("HtmlExportCssPage"); //$NON-NLS-1$
		setTitle(Messages.HtmlExportCssPage_WizardTitle);
		setDescription(Messages.HtmlExportCssPage_CssPageDescription);
	}

	@Override
	public void createControl(Composite parent) {
		mContainer = new Composite(parent, SWT.NULL);
		
		GridLayout gl = new GridLayout(2, false);
		mContainer.setLayout(gl);
		
		Group cssTypeGroup = new Group(mContainer, SWT.NONE);
		cssTypeGroup.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false, 2, 1));
		cssTypeGroup.setLayout(new GridLayout(1, false));
		cssTypeGroup.setText(Messages.HtmlExportCssPage_CSSGroupTitle);
		
		mDefaultCssButton = new Button(cssTypeGroup, SWT.RADIO);
		mDefaultCssButton.setText(Messages.HtmlExportCssPage_DefaultCss);
		mDefaultCssButton.setSelection(true);
		mDefaultCssButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				setCssEnabled(false);
			}
		});
		
		mNoneCssButton = new Button(cssTypeGroup, SWT.RADIO);
		mNoneCssButton.setText(Messages.HtmlExportCssPage_NoCss);
		mNoneCssButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				setCssEnabled(false);
			}
		});
		
		mCustomCssButton = new Button(cssTypeGroup, SWT.RADIO);
		mCustomCssButton.setText(Messages.HtmlExportCssPage_CustomCss);
		mCustomCssButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				setCssEnabled(true);
			}
		});
		
		mCustomCssText = new Text(mContainer, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		mCustomCssText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		
		Composite customCssButtonsGroup = new Composite(mContainer, SWT.NONE);
		customCssButtonsGroup.setLayout(new GridLayout(1, false));
		customCssButtonsGroup.setLayoutData(new GridData(SWT.NONE, SWT.TOP, false, false));
				
		mImportDefaultCss = new Button(customCssButtonsGroup, SWT.PUSH);
		mImportDefaultCss.setText(Messages.HtmlExportCssPage_ImportDefaultCss);
		mImportDefaultCss.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
		mImportDefaultCss.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				importDefaultCss();
			}
		});
		
		mLoadCss = new Button(customCssButtonsGroup, SWT.PUSH);
		mLoadCss.setText(Messages.HtmlExportCssPage_LoadCss);
		mLoadCss.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
				
		setCssEnabled(false);
		
		// Required to avoid an error in the system
		setControl(mContainer);
		setPageComplete(true);
	}
	
	private void importDefaultCss() {
		InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(HtmlGenerator.DEFAULT_CSS_PATH);
		if (stream != null) {
			
			try {
				StringBuilder sb = new StringBuilder();
				InputStreamReader streamReader = new InputStreamReader(stream);
				BufferedReader buffer = new BufferedReader(streamReader);						

				String line = null;
				while ( null != (line = buffer.readLine())){
					sb.append(line + "\n"); //$NON-NLS-1$
				}
				
				mCustomCssText.setText(sb.toString());
			} catch (IOException e) {
				Log.getInstance(HtmlGenerator.class).warn("Unable to include default css."); //$NON-NLS-1$
			} 
		}
	}
	
	private void setCssEnabled(boolean value) {
		mCustomCssText.setEnabled(value);
		mImportDefaultCss.setEnabled(value);
		mLoadCss.setEnabled(value);
	}
	
	public boolean getIncludeCss() {
		return mNoneCssButton.getSelection() ? false : true;
	}
	
	public String getCustomCssText() {
		return mCustomCssButton.getSelection() ? mCustomCssText.getText() : null;
	}

}
